//
//  SSDKPluginImageUtil.m
//  SSDKPlugin-demo
//
//  Created by 涂俊 on 2017/7/18.
//  Copyright © 2017年 涂俊. All rights reserved.
//

#import "SSDKPluginUIUtil.h"

@implementation SSDKPluginUIUtil

+ (UIViewController *)getCurrentVC {
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    NSArray *subViews = [window subviews];
    if (subViews == nil || [subViews count] == 0) {
        return nil;
    }
    
    UIView *frontView = [subViews objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        result = nextResponder;
    } else {
        result = window.rootViewController;
    }
    
    return result;
}


+ (CGFloat)getSystemScale {
    CGFloat scale_screen = [UIScreen mainScreen].scale;
    //不支持iphone3GS的情况
    if (scale_screen==1) {
        scale_screen=2;
    }
    //使用iphone4s的宽度为基准值
    CGFloat baseValue=320;
    int baseScale=2;
    CGRect rx = [UIScreen mainScreen].bounds;
    CGFloat width=rx.size.width;
    CGFloat height=rx.size.height;
    
    //处理iphone6 plus的情况，奇葩的分辨率
    if(scale_screen==3&&((width==414&&height==736)||(width==736&&height==414)))
        scale_screen=2.46;
    //计算缩放比例
    CGFloat v=(height>width?width:height)/baseValue;
    //不支持iphone3GS的情况
    if (v<1) {
        v=1;
    }
    
    v = v*(scale_screen/baseScale);
    //测试6p
    //    v=1.5913125;
    //    scale_screen=2.46;
    //测试6
    //    v=1.171875;
    return 1.0 / scale_screen * v;
}

//ios的坐标系统只识别point，需要转换为像素
+ (CGFloat)getUISize:(CGFloat)value {
    return value * [self getSystemScale];
}

@end
